package argo.hot100.recall;

import java.util.ArrayList;
import java.util.List;

public class Partition {

    public List<List<String>> partition(String s) {
        List<List<String>> res = new ArrayList<>();
        recall(s, res, new ArrayList<>(), new StringBuffer(), 0);
        return res;
    }

    public void recall(String str, List<List<String>> res, List<String> subres, StringBuffer path, int index) {

        if (index >= str.length()) {
            return;
        }

        path.append(str.charAt(index));

        if (path.length() > 0 && isRecev(path.toString())) {
            subres.add(path.toString());
            path.delete(0, path.length() - 1);

        }


        if (index == str.length() - 1 && subres.size() > 0) {
            res.add(new ArrayList<>(subres));
            return;
        }

        recall(str, res, subres, path, index + 1);
        if (path.length() > 0) {
            path.deleteCharAt(path.length() - 1);
        }

    }

    public boolean isRecev(String str) {
        int left = 0, right = str.length() - 1;
        while (left <= right) {
            if (str.charAt(left) != str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}
